package BeenChina.PM.Action;

import BeenChina.PM.Model.ModelRole;
import BeenChina.PM.Service.ServiceFactory;
import BeenChina.PM.Util.Permission;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class RoleInfoModifyAction extends ActionSupport{
	
	private static final long serialVersionUID = 1L;
	
	private String roleName,roleID,id;
	private String oper;
	//DB permission
 
	//page permission
	private String permissions;
	
	 //RenderSet set = RenderSet.DataUri | RenderSet.GZip;
	 //EnumSet.of(RenderSet.DataUri, RenderSet.GZip);

	public void setId(String value)
	{
		id = value;
	}
	public void setOper(String value)
	{
		oper = value;
	}
	
	public void setRoleID(String value)
	{ roleID = value; }	
 
	public void setRoleName(String value)
	{ roleName = value;}
	
	public void setPermissions(String value)
	{ permissions = value;}
	
	public String getPermissions()
	{ return permissions;}
	
	
	
	public String execute() {
		
		 String result = ActionSupport.SUCCESS;	
		  try {
				
			
			  if(oper != null && oper.equals("edit")){  //�༭
				   
				 updateRole();
				  
			  }
			  else if (oper != null && oper.equals("add")){  //����
			        
				  if (existRole()>0)
				  {
					  result = ActionSupport.ERROR;
				  }
				  else {
					  insetRole();
				   }
			  }
			  else  if(oper != null && oper.equals("del")){  //�༭
				   
				  deleteRole();			  
			  }
			}
			catch (Exception ex)
			{
			    ex.printStackTrace();
			    result = ActionSupport.ERROR;
			}
		  
			 return result;
		 }

	private Integer insetRole()
	 {
		 ModelRole modelRole = new ModelRole(roleID,roleName,getDbpermis(permissions));
		 
		 return ServiceFactory.getInstance().getSystemInfoService().insertRole(modelRole);
		 
	 }
	 
	private Integer existRole()
	 {
		 return ServiceFactory.getInstance().getSystemInfoService().existRole( roleID);
	 }
	 
	private Integer updateRole()
	 {
		ModelRole modelRole = new ModelRole(id,roleName,getDbpermis(permissions));
		 
		 return ServiceFactory.getInstance().getSystemInfoService().updateRole(modelRole);
	 }
	 
	private Integer deleteRole()
	{
		 return ServiceFactory.getInstance().getSystemInfoService().deleteRole(id);
	}
	
 
	public String getPremissionArray()
	{
 		
		this.permissions = Permission.getPremissionsNameString();
		return Action.SUCCESS;
	}
	
 
	private String getDbpermis(String permis)
	{ 
		 
		String result = Permission.getItemID(permis);
		return result;
	}
}

